import React, {Component, PropTypes} from 'react';
import {StyleSheet, View, Image, Text} from 'react-native';
import {screenWidth} from './MyExports.js';

/**
 * 主页中ListView的通用Item
 * Created by 10691 on 17/5/17.
 */
export default class MainListItem extends Component {

    static propTypes = {
        image: PropTypes.string,
        title: PropTypes.string,
        mall: PropTypes.string,
        pubTime: PropTypes.string,
        fromSite: PropTypes.string,
    };

    render() {
        return (
            <View style={MainItemStyles.containerStyle}>
                {/*左边图片*/}
                <Image source={{uri: this.props.image === '' ? 'default_thumb_250x250' : this.props.image}}
                       style={MainItemStyles.imageStyle}/>
                {/*中间的标题、发布来源、时间以及来源*/}
                <View style={MainItemStyles.centerViewStyle}>
                    <Text numberOfLines={3} style={MainItemStyles.titleStyle}>{this.props.title}</Text>
                    <View style={MainItemStyles.detailViewStyle}>
                        <Text style={MainItemStyles.mallStyle}>{this.props.mall}</Text>
                        <Text
                            style={MainItemStyles.pubTimeStyle}>{this.props.pubTime}﹒{this.props.fromSite}</Text>
                    </View>
                </View>

                {/*右边的箭头*/}
                <Image source={{uri: 'icon_cell_rightArrow'}} style={MainItemStyles.arrowStyle}/>
            </View>
        );
    }

    /**
     * 计算时间差
     * @param pubTime 发布时间
     * @returns {*}
     */
    switchTime(pubTime) {
        // 时间差的计算
        let minute = 1000 * 60;   // 1分钟
        let hour = minute * 60;   // 1小时
        let day = hour * 24;      // 1天
        let week = day * 7;       // 1周
        let month = day * 30;     // 1个月

        // 计算时间差
        let currentTime = new Date().getTime();
        let diffTime = currentTime - Date.parse(pubTime.replace(/-/gi, "/"));

        if (diffTime < 0) {
            return;
        }

        let diffMonth = diffTime / month;  // 相差几个月
        let diffWeek = diffTime / week;   // 相差几周
        let diffDay = diffTime / day;       // 相差几天
        let diffHour = diffTime / hour;     // 相差几小时
        let diffMinute = diffTime / minute; // 相差几分钟

        if (diffMonth >= 1) {
            return parseInt(diffMonth) + '月前';
        } else if (diffWeek >= 1) {
            return parseInt(diffWeek) + '周前';
        } else if (diffDay >= 1) {
            return parseInt(diffDay) + '天前';
        } else if (diffHour >= 1) {
            return parseInt(diffHour) + '小时前';
        } else if (diffMinute >= 1) {
            return parseInt(diffMinute) + '分钟前'
        } else {
            return '刚刚';
        }
    }
}

const MainItemStyles = StyleSheet.create({

    containerStyle: {
        flex: 1,
        flexDirection: 'row',
        width: screenWidth,
        height: 100,
        backgroundColor: 'white',
        borderBottomWidth: 0.5,
        borderBottomColor: 'gray',
        alignItems: 'center',
        justifyContent: 'space-between',
    },

    imageStyle: {
        width: 80,
        height: 80,
        marginLeft: 10,
    },

    centerViewStyle: {
        marginLeft: 10,
        justifyContent: 'space-around',
        height: 80,
        width: screenWidth * 0.65,
    },

    detailViewStyle: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        alignItems: 'center',
        width: screenWidth * 0.65,
    },

    mallStyle: {
        fontSize: 10,
        color: '#347818',
    },

    pubTimeStyle: {
        fontSize: 10,
    },

    arrowStyle: {
        width: 10,
        height: 10,
        marginLeft: 10,
        marginRight: 10,
    }
});